#include<bits/stdc++.h>
using namespace std;
const int N=5e4+5;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-48,ch=getchar();}
	return x*f;
}
int n,q,p[N],B;
vector<int> e[N];
int fa[N],siz[N],id[N],rnk[N],idx,dep[N],top[N],son[N];

void dfs1(int u,int f){
	dep[u]=dep[f]+1;
	id[u]=++idx,siz[u]=1;
	rnk[idx]=u;
	for(int v:e[u]){
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]]) son[u]=v;
	}
}
void dfs2(int u,int tp){
	top[u]=tp;
	if(!son[u]) return;
	dfs2(son[u],tp);
	for(int v:e[u])
		if(v!=son[u])
			dfs2(v,v);
}
inline int Lca(int x,int y){
	while(top[x]^top[y]){
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		x=fa[top[x]];
	}
	return dep[x]<dep[y]?x:y;
}

struct Que{
	int l,r,id;
}que[N];

inline bool cmp(Que a,Que b){
	if((a.l-1)/B==(b.l-1)/B) return a.r<b.r;
	return a.l<b.l;
}

int ans,res[N],mp[N];
set<int> s;

inline void add_lca(int x){
	if(mp[id[x]]==0) ans++,s.insert(id[x]);
	mp[id[x]]++;
}
inline void del_lca(int x){
	mp[id[x]]--;
	if(!mp[id[x]]) ans--,s.erase(id[x]);
}
inline void add(int x){
	add_lca(x);
	auto it=s.lower_bound(id[x]);
	auto L=--s.lower_bound(id[x]);
	auto R=++s.lower_bound(id[x]);
	int pl=rnk[(it==s.begin())?0:(*L)];
	int pr=rnk[(R==s.end())?0:(*R)];	
	if(pl&&pr) del_lca(Lca(pl,pr));
	if(pl) add_lca(Lca(pl,x));
	if(pr) add_lca(Lca(x,pr));	

}
inline void del(int x){
	auto it=s.lower_bound(id[x]);
	auto L=--s.lower_bound(id[x]);
	auto R=++s.lower_bound(id[x]);
	int pl=rnk[(it==s.begin())?0:(*L)];
	int pr=rnk[(R==s.end())?0:(*R)];	
	if(pl) del_lca(Lca(pl,x));
	if(pr) del_lca(Lca(x,pr));
	del_lca(x);
	if(pl&&pr) add_lca(Lca(pl,pr));
}

signed main(){
	freopen("escape.in","r",stdin);
	freopen("escape.out","w",stdout);
	n=read(),q=read(),B=300;
	for(int i=2;i<=n;++i){
		fa[i]=read();
		e[fa[i]].emplace_back(i);
	}
	for(int i=1;i<=n;++i) p[i]=read();

	dfs1(1,0);
	dfs2(1,1);
	for(int i=1;i<=q;++i){
		que[i].l=read(),que[i].r=read();
		que[i].id=i;
	}
	sort(que+1,que+1+q,cmp);

	int L=1,R=0;
	for(int i=1;i<=q;++i){
		// cerr<<"QJ:"<<que[i].l<<" "<<que[i].r<<"\n";		
		while(R<que[i].r) add(p[++R]);
		while(L<que[i].l) del(p[L++]);
		while(R>que[i].r) del(p[R--]);
		while(L>que[i].l) add(p[--L]);
		res[que[i].id]=ans;
	}
	for(int i=1;i<=q;++i) printf("%d\n",res[i]);
	return 0;
}
/*
5 2
1 1 3 3
1 2 3 4 5
3 5
2 4
*/